package com.zidian.teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.QuestionnaireListBean;

import java.util.List;



/**
 * Created by Administrator on 2016/8/24 0024.
 */
public class QuestionnaireAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List<QuestionnaireListBean.DataBean.ListBean> data;
    private static boolean hasMore = true;


    public QuestionnaireAdapter(Context context, List<QuestionnaireListBean.DataBean.ListBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_question_list, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).questionTitle.setText(data.get(position).getQuestionnaireTitle());
            ((ItemViewHolder) holder).questionRead.setText(data.get(position).getChooseNumber() + "");
            ((ItemViewHolder) holder).questionNoRead.setText(data.get(position).getNotChooseNumber() + "");
            ((ItemViewHolder) holder).questionTime.setText(data.get(position).getReleaseTime()+ "");

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }


        } else if (holder instanceof FootViewHolder) {
            if (!hasMore) {
                if (data.size() > 10) {
                    ((FootViewHolder) holder).loadText.setText("全部数据加载完成!");
                } else {
                    ((FootViewHolder) holder).loadText.setVisibility(View.GONE);
                }
                ((FootViewHolder) holder).progressBar.setVisibility(View.GONE);
            } else {
                ((FootViewHolder) holder).loadText.setText("正在加载...");
                ((FootViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
            }

        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {


        TextView questionTitle;
        TextView questionRead;
        TextView questionNoRead;
        TextView questionTime;

        public ItemViewHolder(View view) {
            super(view);

            questionTitle = (TextView) view.findViewById(R.id.question_list_title);
            questionRead = (TextView) view.findViewById(R.id.question_list_read);
            questionNoRead = (TextView) view.findViewById(R.id.question_list_no_read);
            questionTime = (TextView) view.findViewById(R.id.question_list_time);

        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        TextView loadText;
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            loadText = (TextView) view.findViewById(R.id.load_text);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        }
    }


}
